package com.yhg.qxmall.socket.controller;

import com.alibaba.fastjson.JSONObject;
import com.yhg.qxmall.socket.utils.DateUtil;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.Random;


public class SocketController {
    public static String sendPost(String url,String param){
        OutputStreamWriter out =null;
        BufferedReader reader = null;
        String response = "";

        //创建连接
        try {
            URL httpUrl = null; //HTTP URL类 用这个类来创建连接
            //创建URL
            httpUrl = new URL(url);
            //建立连接
            HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("connection", "keep-alive");
            conn.setUseCaches(false);//设置不要缓存
            conn.setInstanceFollowRedirects(true);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.connect();
            //POST请求
            out = new OutputStreamWriter(
                    conn.getOutputStream());
            out.write(param);
            out.flush();
            //读取响应
            reader = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));
            String lines;
            while ((lines = reader.readLine()) != null) {
                lines = new String(lines.getBytes(), "utf-8");
                response+=lines;
            }
            reader.close();
            // 断开连接
            conn.disconnect();

        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！"+e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(reader!=null){
                    reader.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }

        return response;
    }



    public static void main(String[] args) {
//        氮在bai140-225，磷57-100，钾106-150

        Random rand = new Random();
        JSONObject jsonParam = new JSONObject();
        for (int i = 1; i <=3 ; i++) {

            jsonParam.put("area", i);
            jsonParam.put("date_calculated", DateUtil.AllDate(new Date()));
            jsonParam.put("temperature",rand.nextInt(30)+10);
            jsonParam.put("humidity", rand.nextInt(100)+1);
            jsonParam.put("dan", rand.nextInt(200)+100);
            jsonParam.put("lin", rand.nextInt(90)+40);
            jsonParam.put("jia", rand.nextInt(100)+80);
            String param = jsonParam.toJSONString();
            String url="http://localhost:10086/socket/daily/saveBean";
            String sendPost = sendPost(url, param);
            System.out.println(sendPost);
        }
    }

}